package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("list")
    public R<List<AddressBook>> list(){
        List<AddressBook> list = addressBookService.list();
        return R.success(list);
    }

    @PutMapping("default")
    public R<String> defaultAddr(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddr(addressBook.getId());
        return R.success("修改成功");
    }

    @GetMapping("default")
    public R<AddressBook> getDefaultAddr(){
        AddressBook addressBook = addressBookService.getDefaultAddr();
        return R.success(addressBook);
    }
    /**
     * 获取地址信息
     */
    @GetMapping("{id}")
    public R<AddressBook> getAddr(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改地址信息
     */
    @PutMapping
    public R<String> updateAddr(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
    /**
     * 批量删除删除地址
     */
    @DeleteMapping
    public R<String> deleteAddr(Long[] ids){
        addressBookService.lambdaUpdate()
                .in(AddressBook::getId,ids)
                .remove();
        return R.success("删除成功");
    }

    /**
     * 新建收货地址
     */

    @PostMapping
    public R<String> addAddr(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

}
